﻿@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<MDCard>
    <DataTableCard TModel="RankOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" Title="@($"{_name}列表")" OnClickAdd="AddItem" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <div style="width: 140px;">
                    <MIcon Color="success"
                           Class="mr-2"
                           OnClick="()=>EditItem(context.Item)">@IconType.Edit.ToIconString()</MIcon>
                    <MIcon Color="info"
                           Class="mr-2"
                           OnClick="()=>EditItemPriority(context.Item,true)">@IconType.Up.ToIconString()</MIcon>
                    <MIcon Color="info"
                           Class="mr-2"
                           OnClick="()=>EditItemPriority(context.Item,false)">@IconType.Down.ToIconString()</MIcon>
                    @if (context.Item.IsHidden)
                    {
                        <MIcon Color="warning"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,false)">@IconType.Check.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,true)">@IconType.Close.ToIconString()</MIcon>
                    }
                </div>
            }
            else if (context.Header.Value == nameof(RankOverviewModel.IsHidden))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Recommends.StateChip IsHidden="context.Item.IsHidden" />
            }
            else if (context.Header.Value == nameof(RankOverviewModel.Type))
            {
                <TypeChip Type="context.Item.Type" />
            }
            else if (context.Header.Value == nameof(RankOverviewModel.CSS))
            {
                <CnGalWebSite.Shared.MasaComponent.Shared.Cards.Ranks.RankAloneViewTip Model="new RankViewModel{Text=context.Item.Text,CSS=context.Item.CSS,Styles=context.Item.Styles}" />
            }
            else if (context.Header.Value == nameof(RankOverviewModel.Image))
            {
                <ImageChip Image="@context.Item.Image" />
            }
            else if (context.Header.Value == nameof(RankOverviewModel.CreateTime))
            {
                @context.Item.CreateTime.ToString("yyyy-MM-dd HH:mm")
            }
            else if (context.Header.Value == nameof(RankOverviewModel.LastEditTime))
            {
                @context.Item.LastEditTime.ToString("yyyy-MM-dd HH:mm")
            }

            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>

<MDialog @bind-Value="_editDialog"
         MaxWidth="1200">
    <EditCard Id="_editedItemId" OnEdited="OnEdited" @ref="editCard" />
</MDialog>
@code {
    private string _baseUrl = "api/ranks/ListRanks";
    private string _name = "头衔";

    private List<DataTableHeader<RankOverviewModel>> _headers = new List<DataTableHeader<RankOverviewModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(RankOverviewModel.Id)
          },
          new (){ Text= "类型", Value= nameof(RankOverviewModel.Type)},
          new (){ Text= "名称", Value= nameof(RankOverviewModel.Name)},
          new (){ Text= "数量", Value= nameof(RankOverviewModel.Count),Sortable=false},
          new (){ Text= "样式", Value= nameof(RankOverviewModel.CSS)},
          new (){ Text= "图片", Value= nameof(RankOverviewModel.Image)},
          new (){ Text= "创建时间", Value= nameof(RankOverviewModel.CreateTime)},
          new (){ Text= "最后编辑时间", Value= nameof(RankOverviewModel.LastEditTime)},
          new (){ Text= "优先级", Value= nameof(RankOverviewModel.Priority)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<RankOverviewModel> dataTableCard;

    EditCard editCard;
    private bool _editDialog;
    private long _editedItemId;
    private bool _detailDialog;


    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public async void EditItem(RankOverviewModel item)
    {
        _editedItemId = item.Id;
        _editDialog = true;
        StateHasChanged();
        if (editCard != null)
        {
            await editCard.Refresh();
        }
    }

    public void OnEdited()
    {
        _editDialog = false;
        StateHasChanged();
        if (dataTableCard != null)
        {
            dataTableCard.GetDataFromApi();
        }
    }

    public async Task AddItem()
    {
        _editedItemId = 0;
        _editDialog = true;
        StateHasChanged();
        if (editCard != null)
        {
            await editCard.Refresh();
        }
    }

    public async Task EditItemPriority(RankOverviewModel model, bool up)
    {
        string operationName = up ? "增权重" : "降权重";
        try
        {
            var obj = await _httpService.PostAsync<EditRankPriorityViewModel, Result>("api/ranks/EditRankPriority", new EditRankPriorityViewModel { Ids = new long[] { model.Id}, PlusPriority = up?1:-1 });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }

    public async Task HideItem(RankOverviewModel model, bool isHidden)
    {
        string operationName = isHidden ? "隐藏头衔" : "显示头衔";
        try
        {
            var obj = await _httpService.PostAsync<HiddenRankModel, Result>("api/ranks/HiddenRank", new HiddenRankModel { Ids = new long[] { model.Id }, IsHidden = isHidden });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败",  AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }
}
